$(function(){
	/**调用初始化方法*/
	queryRole();
});
/*function getAllRole(){
	$.post("/GetAllGoods/",null,function(str){
		str = decodeURI(str);
		var data = $.parseJSON(str);
		for(var i=0;i<data.length;i++){
			$("#selectId").append("<option value='"+data[i].RoleId+"'>"+data[i].RoleName+"</option>");
		}
	});
}*/

/*function queryForm(){
	var userName = $("#queryName").val();
	var roleId = $("#querySelect").val();
	queryRole(userName,roleId);
}*/

function queryRole(){
	$(this).hide();
	$('#usertable').bootstrapTable('destroy');
	$('#usertable').bootstrapTable({
		method: 'get',
		url: '/GetAllNews/',
		cache: false,
		striped: true,
		pagination:true,
		pageSize:20,
		pageList: [5, 10, 20,50],
		pageNumber:1,
		sidePagination:'server',
		width:'1200',
		columns: [
			{
				field: 'id',
				title: 'Item ID',
				align: 'right',
				valign: 'bottom',
				visible:false
			},{
				field: 'newsTitle',
				title: '新闻标题',
				align: 'center',
				formatter: turnAround,
				valign: 'middle'

			}, {
				field: 'newsType',
				title: '新闻类型',
				formatter: turnAround,
				align: 'center',
				valign: 'middle'
			},{
				field: 'newsAuthor',
				title: '作者',
				formatter: turnAround,
				align: 'center',
				valign: 'middle'


			},{
				field: 'newsSource',
				title: '新闻来源',
				align: 'center',
				valign: 'middle'

			},{
				field: 'newsState',
				title: '新闻热度',
				align: 'center',
				valign: 'middle'
			},{
				field: 'newsContaint',
				title: '新闻内容',
				align: 'center',
				valign: 'middle',
				visible:false

			},{
				field: 'operate',
				title: '操作',
				align: 'center',
				valign: 'middle',
				formatter: operateFormatter,
				events: operateEvents
			}]
	});
}
/**
 * table图标区
 */
function operateFormatter(value, row, index) {
	if($("#updatex").html()=="0"||$("#up").html()=="0"){
		if($("#updatex").html()=="0"&&$("#up").html()=="0"){
			return [].join('');
		}else if($("#updatex").html()=="0"){
			return [
				'<button class="upGoods" id="up">删除</button>'
				].join('');
		}else if($("#up").html()=="0"){
			return [
				'<button class="updateUser" id="updateUser">修改</button>'
				].join('');
		}
	}
		return [
			'<button class="updateUser" id="updateUser">修改</button>&nbsp',
			'<button class="upGoods" id="up">删除</button>'
			].join('');
}

/**
 * 图标绑定点击事件
 */
window.operateEvents = {
		'click .updateUser': function (e, value, row, index) {
			isModify=1;
			$("#titleId").html("修改用户");
			$(".com_title").val(decodeURI(row.newsTitle,"utf-8"));
			$(".price").val(decodeURI(row.newsAuthor,"utf-8"));
			/*$("#editor").val(decodeURI(row.newsContaint,"utf-8"));*/
			$(".w-e-text").html(decodeURI(row.newsContaint,"utf-8"));
			$(".state").val(row.newsState);
			if(row.newsType=="系统公告"){
				$(".com_type").val("0");
			}else if (row.newsType=="游戏新闻") {
				$(".com_type").val("1");
			}else if (row.newsType=="热点活动") {
				$(".com_type").val("2");
			}else if (row.newsType=="比赛新闻") {
				$(".com_type").val("3");
			}else{
				$(".com_type").val("4");
			}
			$(".com_sum").val(decodeURI(row.newsSource,"utf-8"));
			$("#ids").val(row.id);
			$("#show_div_user").show();
		},
		'click .upGoods': function (e, value, row, index) {
			var id=row.id;
			if(confirm("你确定提交该操作吗？")){
				$.get("/deleteNews/?id="+id,null,function(data){
					alert(data);
					queryRole();
				});
			}
		}
};

function turnAround(value, row, index){
	return decodeURI(value,"utf-8"); 
}



/* 点击提交的事件  */
$("#addUser").click(function(){
	$(".show_div").hide();
	var data={};
	var newsTitle=$(".com_title").val();
	var newsAuthor=$(".price").val();
	var newsState=$(".state").val();
	var newsType=$(".com_type").val();
	var newsSource=$(".com_sum").val();
	var newsContaint=$(".w-e-text").html();
	data.newsTitle = newsTitle;
	data.newsAuthor = newsAuthor;
	data.newsState = newsState;
	data.newsType = newsType;
	data.newsSource = newsSource;
	data.newsContaint = newsContaint;
	var userid = $("#ids").val();
	if(userid!=0 && userid!=null){
		data.id = userid;
	}
	if(newsTitle&&newsAuthor&&newsState&&newsType
			&&newsContaint){
			if(confirm("你确定提交该操作吗？")){
				$(this).html('发送中...');
				$(this).css({"background":"#616161"});
				$.post("/addNews/",data,function(data){
					data = decodeURI(data);
					var data1 = $.parseJSON(data);
					if(data1.state == 1){
						window.location.reload();
						alert(data1.message);
					}else{
						$(".showList_submit").html('提交');
						$(".showList_submit").css({"background":"#438EB9"});
						alert(data1.message);
						$("#show_div_user").show();
					}
				});
			}
	}else{
		alert("填写错误");
	}
});
